using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextMessagesDisplay : MonoBehaviour
{
	public UISprite background;

	public List<UILabel> labels = new List<UILabel>();

	public GameObject message2Container;

	public UILabel message2Label;

	protected UIWidget container;

	private bool initialized_;

	public float scaleInDuration = 1f;

	public float scaleInLabelDuration = 0.25f;

	public float delayBetweenChangeText = 0.25f;

	public float changeTextDuration = 0.5f;

	public float delayBeforeFadeOut = 1f;

	public float fadeOutDuration = 0.25f;

	protected float maxBackgroundAlpha;

	private float oldFadeDuration;

	private void Init()
	{
		if (!initialized_)
		{
			base.gameObject.SetActive(false);
			initialized_ = true;
			container = GetComponent<UIWidget>();
			maxBackgroundAlpha = background.alpha;
		}
	}

	private void Awake()
	{
		Init();
	}

	public void DemoShow()
	{
		ShowTextChange("1 - 1", "1 - 2", null);
	}

	public void ShowScoreChange(string prefix, int score1, int score2, int tagThatWonPoint, string message2, Action onDone)
	{
		UITools.SetActive(message2Container, !string.IsNullOrEmpty(message2));
		UITools.ChangeText(message2Label, message2);
		int num = score1;
		int num2 = score2;
		if (tagThatWonPoint == 0)
		{
			num--;
		}
		else
		{
			num2--;
		}
		ShowTextChange(prefix + num + " : " + num2, prefix + score1 + " : " + score2, onDone);
	}

	public void ShowText(string text, Action onDone, float fadeDelay)
	{
		oldFadeDuration = delayBeforeFadeOut;
		delayBeforeFadeOut = fadeDelay;
		UITools.SetActive(message2Container, false);
		ShowTextChange(text, null, delegate
		{
			delayBeforeFadeOut = oldFadeDuration;
			if (onDone != null)
			{
				onDone();
			}
		});
	}

	public void ShowTextChange(string text1, string text2, Action onDone)
	{
		Init();
		base.gameObject.SetActive(true);
		StopAllCoroutines();
		StartCoroutine(DoShowTextChange(text1, text2, onDone));
	}

	public UILabel GetLabel(int labelIndex)
	{
		return labels[labelIndex % labels.Count];
	}

	private IEnumerator DoShowTextChange(string text1, string text2, Action onDone)
	{
		if (message2Container == null)
		{
			base.gameObject.SetActive(false);
			onDone();
			yield break;
		}
		container.alpha = 1f;
		foreach (UILabel label3 in labels)
		{
			label3.alpha = 0f;
		}
		int labelIndex = 0;
		Vector3 message2ContainerStartScale = new Vector3(1f, 0f, 1f);
		message2Container.transform.localScale = message2ContainerStartScale;
		yield return StartCoroutine(ScaleInBackground(scaleInDuration));
		StartCoroutine(ScaleAndFade(message2Container.GetComponent<UIWidget>(), message2ContainerStartScale, Vector3.one, 0.5f, 1f, 0.25f));
		UILabel label1 = GetLabel(labelIndex);
		label1.text = text1;
		yield return StartCoroutine(ScaleAndFade(label1, new Vector3(1f, 0f, 1f), Vector3.one, 0f, 1f, scaleInLabelDuration));
		yield return new WaitForSeconds(delayBetweenChangeText);
		if (text2 != null)
		{
			StartCoroutine(ScaleAndFade(label1, Vector3.one, Vector3.one, 1f, 0f, changeTextDuration));
			UILabel label2 = GetLabel(labelIndex + 1);
			label2.text = text2;
			yield return StartCoroutine(ScaleAndFade(label2, Vector3.one, Vector3.one, 0f, 1f, changeTextDuration));
		}
		else
		{
			yield return new WaitForSeconds(changeTextDuration);
		}
		yield return new WaitForSeconds(delayBeforeFadeOut);
		StartCoroutine(ScaleAndFade(background, Vector3.one, new Vector3(1f, 0f, 1f), maxBackgroundAlpha, 0f, fadeOutDuration));
		yield return StartCoroutine(ScaleAndFade(container, Vector3.one, Vector3.one, 1f, 0f, fadeOutDuration));
		base.gameObject.SetActive(false);
		if (onDone != null)
		{
			onDone();
		}
	}

	private IEnumerator ScaleInBackground(float duration)
	{
		background.width = container.width;
		Transform bckTrans = background.cachedTransform;
		Vector3 pos = bckTrans.localPosition;
		Vector2 pivotOffset = NGUIMath.GetPivotOffset(background.pivot);
		pos.x = (float)background.width * (pivotOffset.x - 0.5f);
		bckTrans.localPosition = pos;
		float horizontalScaleDuration = duration * 0.5f;
		float verticalScaleDuration = duration * 0.5f;
		Vector3 startHorizontalScale = new Vector3(0f, 0.2f, 1f);
		Vector3 endHorizontalScale = startHorizontalScale + Vector3.right;
		Vector3 startVerticalScale = endHorizontalScale;
		yield return StartCoroutine(ScaleAndFade(endScale: Vector3.one, widget: background, startScale: new Vector3(0f, 1f, 1f), startAlpha: maxBackgroundAlpha, endAlpha: maxBackgroundAlpha, duration: verticalScaleDuration * 2f));
	}

	private IEnumerator ScaleAndFade(UIWidget widget, Vector3 startScale, Vector3 endScale, float startAlpha, float endAlpha, float duration)
	{
		float time = 0f;
		while (time < duration)
		{
			float normTime = time / duration;
			widget.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
			widget.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, normTime);
			time += RealTime.deltaTime;
			yield return null;
		}
		widget.alpha = endAlpha;
		widget.cachedTransform.localScale = endScale;
	}
}
